package com.mago.tis.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mago.tis.bean.TradersBean;
import com.mago.tis.dao.ITraderDao;
import com.mago.tis.daoImpl.TraderDao;
import com.mago.tis.pojo.Tistit;
import com.mago.tis.service.ITraderService;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.ExceptionMessage;

public class TraderService extends BaseService implements ITraderService{
	
	private static final Logger logger = Logger.getLogger(TraderService.class);
	
	public boolean addTraders(TradersBean traderBean)
			throws ApplicationException {
		
		ITraderDao traderDao=new TraderDao();
		boolean isSaved=false;
		if(traderBean!=null){
			try{
				Tistit tit=new Tistit();
				tit.setCompanyName(traderBean.getName());
				tit.setAddress(traderBean.getAddress());
				tit.setContactNo(traderBean.getContactNo());
				tit.setEmailId(traderBean.getEmailId());
				tit.setDesc(traderBean.getDesc());
				
				tit.setMobileNo(traderBean.getMobileNo());
				tit.setPanNo(traderBean.getPanNo());
				
				tit.setBankName(traderBean.getBankName());
				tit.setAccountNo(traderBean.getAccountNo());
				tit.setIfscCode(traderBean.getIfscCode());
				tit.setMicrCode(traderBean.getIfscCode());
				
				tit.setMulFactor(traderBean.getMulFactor());	
				tit.setLandingCost(traderBean.getLandingCost());
				tit.setExtraField1(traderBean.getExtraField1());
				isSaved=traderDao.saveTrader(tit);
			}
			catch (ApplicationException ae) {
				throw ae;
			} catch (Exception e) {
				logger.error("TraderService : addTraders() ----> "
						+ e.getMessage());
				throw new ApplicationException(
						ExceptionMessage.ADD_TRADER_UNABLE);
			}
		}
		
		return isSaved;
	}

	public boolean deleteTraders(String companyId) throws ApplicationException {
		ITraderDao traderDao=new TraderDao();
		boolean isDelete=false;
		try{
			isDelete=traderDao.deleteTradersOnId(companyId);
		} 
		catch (ApplicationException ae) {
			logger.error("TraderService : deleteTraders() ----> ");
			throw ae;
		}
		return isDelete;
	}

	public List<Tistit> getTraders() throws ApplicationException {
		ITraderDao traderDao=new TraderDao();
		List<Tistit> list_traders=new ArrayList<Tistit>();
		try{
			list_traders=traderDao.getAllTraders();
			logger.info("TraderService : getTraders() Traders List--> "+list_traders.size());
		} 
		catch (ApplicationException ae) {
			logger.error("TraderService : getTraders() ----> ");
			throw ae;
		}
		return list_traders;
	}

	public List<Tistit> getTradersById(String companyId)
			throws ApplicationException {
		ITraderDao traderDao=new TraderDao();
		List<Tistit> list_traders=new ArrayList<Tistit>();
		try{
			list_traders=traderDao.getTradersOnId(companyId);
			logger.info("TraderService : getTradersById() Traders List--> "+list_traders.size());
		} 
		catch (ApplicationException ae) {
			logger.error("TraderService : getTradersById() ----> ");
			throw ae;
		}
		return list_traders;
	}

	public boolean updateTraders(TradersBean traderBean)
			throws ApplicationException {
		ITraderDao traderDao=new TraderDao();
		boolean isUpdated=false;
		if(traderBean!=null){
			try{
				Tistit tit=new Tistit();
				tit.setCompanyId(Integer.parseInt(traderBean.getCompanyId()));
				tit.setCompanyName(traderBean.getName());
				tit.setAddress(traderBean.getAddress());
				tit.setContactNo(traderBean.getContactNo());
				tit.setEmailId(traderBean.getEmailId());
				tit.setDesc(traderBean.getDesc());
				tit.setMobileNo(traderBean.getMobileNo());
				tit.setPanNo(traderBean.getPanNo());
				
				tit.setBankName(traderBean.getBankName());
				tit.setAccountNo(traderBean.getAccountNo());
				tit.setIfscCode(traderBean.getIfscCode());
				tit.setMicrCode(traderBean.getMicrCode());
				
				
				tit.setMulFactor(traderBean.getMulFactor());
				tit.setLandingCost(traderBean.getLandingCost());
				tit.setExtraField1(traderBean.getExtraField1());
				
				isUpdated=traderDao.updateTrader(tit);
			}
			catch (ApplicationException ae) {
				throw ae;
			} catch (Exception e) {
				logger.error("TraderService : updateTraders() ----> "
						+ e.getMessage());
				throw new ApplicationException(
						ExceptionMessage.UPDATE_TRADER_UNABLE);
			}
		}
		
		return isUpdated;

	}

}
 